
package com.catsic.zcjc.web.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.annotation.Log;
import com.catsic.base.controller.BaseController;
import com.catsic.zcjc.condition.MarkLabelCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.entity.MarkLabel;
import com.catsic.zcjc.form.MarkLabelForm;
import com.catsic.zcjc.service.MarkLabelService;


@Controller
@RequestMapping("/mark")
public class MarkController extends BaseController{
	
	@Autowired
	private MarkLabelService markLabelService;

	
    /**
     * 查询标签
	 * @author xietiebing
	 * @param condition 
	 * @return
	 */
	@RequestMapping(value = "/query" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> query(MarkLabelCondition condition,Pageable  pageable){
		condition.setCreateId(getCurrentUser().getUserId());
		Page<MarkLabelForm> page = markLabelService.findAll(condition, pageable);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, page);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * create:创建
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "标签管理", operation = "添加标签")
	public Map<String,Object> create(MarkLabelForm form){
		form.setCreateId(getCurrentUser().getUserId());
		form.setCreateName(getCurrentUser().getUsername());
		
		form = markLabelService.add(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author xietiebing
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "标签管理", operation = "修改标签")
	public Map<String,Object> update(MarkLabelForm form){
		if(form.getType()==ZcjcConstants.LABEL_TYPE_PRIVATE){
			form.setCreateId(getCurrentUser().getUserId());
		}
		form = markLabelService.update(form);
		Map<String,Object> map = new HashMap<String,Object>(); 
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	

	/**
	 * detail:详情
	 * @author xietiebing
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> detail(String id){
		MarkLabelForm form =markLabelService.findMarkLabelById(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}

	/**
	 * delete:删除
	 * @author xietiebing
	 * @param id
	 * @return 
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	@Log(module = "标签管理", operation = "删除标签")
	public Map<String,Object> delete(String id){
		markLabelService.delete(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(RESULT,RESULT_SUCCESS);
		return map;
	}

	/**
	 * all:详情
	 * @author xietiebing
	 * @return
	 */
	@RequestMapping(value = "/all.json" , method = RequestMethod.GET)
	@ResponseBody
	public List<MarkLabel> all(){
		List<MarkLabel> marks = markLabelService.findByUser(getCurrentUser().getUserId());
		return marks;
	}
	
}
